﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameLib.Serialization;

namespace Sandbox.Serialization
{
	public abstract class StringSerializer : Serializer
	{
		public override DataType DataType
		{
			get { return GameDataTypes.Text; }
		}

		public override bool Deserialize(object obj, string dataPath)
		{
			throw new NotImplementedException();
		}

		public override bool Serialize(object obj, string dataPath)
		{
			throw new NotImplementedException();
		}

		public virtual object ConvertFromString(object obj, Type type, string str, DataFlags dataFlags)
		{
			if (CanOnlyConvertToNewObject)
			{
				if (obj != null)
					throw new Exception(string.Format("{0} cannot serialize string into a given object, it has to create new object", GetType().Name));

				return FromString(type, str, dataFlags);
			}
			else
			{
				throw new NotImplementedException();
			}
		}

		public virtual bool CanOnlyConvertToNewObject
		{
			get { return true; }
		}

		protected abstract object FromString(Type type, string str, DataFlags dataFlags);


		public abstract string ConvertToString(object obj, DataFlags dataFlags);

		protected const string NullString = "null";
	}

	public abstract class StringSerializer<T> : StringSerializer where T:class
	{
		public override Type SupportedType
		{
			get { return typeof(T); }
		}

		public override string ConvertToString(object obj, DataFlags dataFlags)
		{
			return ConvertToStringTyped(obj as T, dataFlags);
		}

		public abstract string ConvertToStringTyped(T obj, DataFlags dataFlags);
	}
}
